package com.xing.photo.dao.impl;

import com.xing.photo.bean.PhotoUser;
import com.xing.photo.dao.UserDao;
import org.mybatis.spring.SqlSessionTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: xingzhe
 * Date: 2015/3/2
 * Time: 11:45
 */
public class UserDaoImpl implements UserDao {
    @Override
    public List<PhotoUser> listUsers(Integer limit) {
        if (limit == null) {
            return (List<PhotoUser>) sqlSession.selectList(getClass().getName() + ".listUsers");
        } else {
            Map map = new HashMap();
            map.put("limit", limit);
            return (List<PhotoUser>) sqlSession.selectList(getClass().getName() + ".listUsers", map);
        }
    }

    @Override
    public int updateUser(PhotoUser user) {
        return sqlSession.update(getClass().getName() + ".updateUser", user);
    }

    @Override
    public int saveUser(PhotoUser user) {
        return sqlSession.insert(getClass().getName() + ".saveUser", user);
    }

    @Override
    public PhotoUser getUser(int id) {
        return (PhotoUser) sqlSession.selectOne(getClass().getName() + ".getUser", id);
    }

    @Override
    public int delUser(PhotoUser user) {
        return sqlSession.update(getClass().getName() + ".delUser", user);
    }

    public SqlSessionTemplate sqlSession;

    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }
}
